home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6633 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  70 lines

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Confusion as to the proper use of MODULAS
  5. Date: Wed, 14 Feb 1996 23:59:46 GMT
  6. Organization: Netcom
  7. Message-ID: <312266e3.78899070@nntp.ix.netcom.com>
  8. References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> <4fthsu$1kl@ixnews7.ix.netcom.com>
  9. NNTP-Posting-Host: ix-dc10-04.ix.netcom.com
  10. X-NETCOM-Date: Wed Feb 14  3:59:27 PM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. wzjn@ix.netcom.com (KPN ) wrote:
  14.  
  15. > Confusion as to the proper use of MODULAS
  16. > Very confused. Just as I think IÆve got it correct, I make-up a new
  17. > test, the results of which leave me mixed-up again.
  18. > To begin with, this is the first rule I found out:
  19. > Modulas operator simply shows:  *** AFTER *** you divide, what is the
  20. > remainder?
  21. > EXAMPLE of this:
  22. >     9 % 7 = ?
  23. >     9 divided by 7 yields a remainder of 2
  24. >     9 % 7 = 2
  25. > 1) MODULAS means divide a number and give me the left over number. OK -
  26. > not bad.
  27. > Next rule:
  28. > If the number to be divided is smaller than the number to divide by,
  29. > the result will always be the number being divided
  30. > EXAMPLE of this:
  31. >    9 % 12 = ?
  32. >    can not divide 9 by a larger number
  33. >    9 % 12 = 9
  34. > Now I have rule #2
  35. > 2) If the number to be divided is smaller than the nuber to be used as
  36. > a divisor, the result is the original number. OK - again, not bad.
  37. > Here, begin my troubles: using MODULAS in an IF statement.
  38. > I made up a test to see if a number I inputted was a 7. Sample code:
  39. >    if (number % 7)
  40. >       printf("Not a 7\n");
  41. >    else
  42. >       printf("First integer was a 7\n");
  43. > So, if the number WAS a 7, the ELSE would take over. OK. But, when I
  44. > enter in a ZERO, it still tells me that the number was a seven?
  45. > Am I going in the right direction? Can someone tell me what IÆm doing
  46. > incorrectly, or where IÆm straying? WhatÆs the rule here for using MOD
  47. > in an IF statement?
  48.  
  49. The problem is that you're printing the wrong message.  If number is
  50. nonnegative, number % 7 is the remainder when dividing number by 7.
  51. The remainder when dividing 0 by 7 is 0.
  52.  
  53. In any case, if a divides b then b % a is 0.  Your code should print
  54. that the integer was a 7 if it is ..., -14, -7, 0, 7, 14, ...
  55.  
  56.  
  57. Michael M Rubenstein
  58.